package com.hspedu.exit_;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/15
 * @time 17:24
 */
public class ThreadMethod03 {
    public static void main(String[] args) throws InterruptedException {
        MyDaemonThread myDaemonThread = new MyDaemonThread();
        /**
         * 我们希望main线程结束后
         * 子线程自动结束
         * 我们只需将子线程设置为守护线程即可
         */
        myDaemonThread.setDaemon(true);

        myDaemonThread.start();
        for (int i = 1; i <= 10 ; i++) {
            System.out.println("小明在努力工作");
            Thread.sleep(500);
        }
    }
}

class MyDaemonThread extends Thread{
    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("小明和小红正在聊天...");
        }
    }
}